package Thread;

/**
 * @description: RunnableStop
 * 终止线程
 * 1.内部结束 ：循环次数结束
 * 2.关联标识
 * 3.外部干涉结束 ：调用外部函数结束
 * 合并线程  xx.join()
 *
 * @date: 2019/12/19 19:03
 * @author: Vicuna
 */


public class RunnableStop implements Runnable {
    private boolean flag = true;
    private String name;

    @Override
    public void run() {
        int k = 0;
        // 关联标识结束
        while (flag) {
            //内部结束
            if (k == 1000)
                break;

            System.out.println(name + "--->" + k++);
        }
    }

    public RunnableStop(String name) {
        this.name = name;
    }

    //外部干涉结束
    public void change() {
        this.flag = false;
    }

    public static void main(String[] args) {
        RunnableStop man = new RunnableStop("小明");
        new Thread(man).start();
        for (int i = 0; i < 500; i++) {
            if (i == 489) {
                 man.change();           //线程终止
                System.out.println("小明Death");
            }
            System.out.println("循环次数" + "--->" + i);
        }
    }
}
